package com.example.blogsystem.config;

import com.example.blogsystem.exception.BlogException;
import com.example.blogsystem.exception.UserException;
import com.example.blogsystem.model.common.Result;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Result error(Exception e){
        e.printStackTrace();
        return Result.fail("未知异常");
    }

    @ExceptionHandler(BlogException.class)
    @ResponseBody
    public Result error(BlogException e){
        return Result.fail(e.getMessage());
    }

    @ExceptionHandler(UserException.class)
    @ResponseBody
    public Result error(UserException e){
        return Result.fail(e.getMessage());
    }
}
